package XMLread;

import java.util.ArrayList;
import android.util.SparseArray;
import com.google.android.maps.GeoPoint;

/**
 * This class defines an indoor map of a Building.  This building can be traversed to obtain routes within
 * @author shullc74
 *
 */
public class Building {
	private String buildingID;
	private ArrayList<Floor> floors;
	private SparseArray<Floor> floorArray;
	private ArrayList<String> exitFloors;

	/**
	 * Default Constructor
	 * 	constructs a new empty Building
	 */
	public Building(){
		buildingID = "";
		floors = new ArrayList<Floor>();
		floorArray = new SparseArray<Floor>();
		exitFloors = new ArrayList<String>();
	}

	/**
	 *  @return returns the building ID of this building
	 */
	public String getBuildingID(){
		return buildingID;
	}

	/**
	 *  @param id sets the building ID to id 
	 */
	public void setBuildingID(String id){
		buildingID = id;
	}

	/**
	 *  @return returns a list of references to Floors in the building
	 */
	public ArrayList<Floor> getFloors(){
		return floors;
	}

	/**
	 *  @param landing adds the floor to this Building 
	 */
	public void addEntity(Floor landing) {
		floors.add(landing);
		boolean addedExit = false;
		for(Hallway h : landing.getHallways()){
			for(Object o : h.getEntities()){
				if(o instanceof Exit){
					exitFloors.add(landing.getID()+"");
					addedExit = true;
					break;
				}
			}
			if(addedExit)
				break;
		}
	}

	/**
	 *  @return returns a list of references to Floors in the building that contain an Exit
	 */
	public ArrayList<String> getExitFloors(){
		return exitFloors;
	}

	/**
	 *  @param  gp the geopoint location of the Stairwell we are currently at
	 *  @return returns a Stairwell in the same location as the passed parameter but on a floor of the building with an exit
	 *  				returns null if no stairwell exists at this location on an exit floor 
	 */
	public Stairwell containsStairwellsAtLoc(GeoPoint gp){

		for(Floor f : floors){
			for(Hallway h : f.getHallways()){
				for(Object o : h.getEntities()){
					if(o instanceof Stairwell && ((Stairwell) o).getGeopoint().equals(gp) && 
							exitFloors.contains(((Stairwell) o).getCurrentFloor()+"")){
						return (Stairwell) o;
					}
				}
			}
		}
		return null;
	}

	/**
	 *  @param  start the Stairwell we are currently at
	 *  @return returns true if the start Stairwell is located on a floor of this building that has an exit.  Otherwise returns false. 
	 */
	public boolean weOnExitFloor(Stairwell start) {
		String currentFloor = start.getCurrentFloor()+"";
		return exitFloors.contains(currentFloor);
	}

	/**
	 *  @param  geopoint the Stairwell location we are currently at
	 *  @param  endfloor the floor we want to get to
	 *  @return returns a Stairwell in the same location as the passed parameter but on a floor we want to get to
	 *  				returns null if no stairwell exists at this location on the floor we want 
	 */
	public Stairwell hasStairAtGPonFloor(GeoPoint geopoint, int endFloor) {
		Stairwell stair = null;
		Floor f = floorArray.get(endFloor);
		boolean foundStair = false;
		for(Hallway h : f.getHallways()){
			for(Object o : h.getEntities()){
				if(o instanceof Stairwell && ((Stairwell)o).getGeopoint().equals(geopoint)){
					foundStair = true;
					stair = (Stairwell) o;
					break;
				}
			}
			if(foundStair)
				break;
		}
		return stair;
	}

}
